system-controller: premature setting of active and visible attributes
[profile/ivi/murphy.git] / packaging.in / murphy.lua
1 with_system_controller = false
2 with_amb = false
3 verbose = 0
4
5 m = murphy.get()
6
7 -- try loading the various logging plugins
8 m:try_load_plugin('systemd')
9 m:try_load_plugin('dlog')
10
11 -- load the console plugin
12 m:try_load_plugin('console')
13
14 m:try_load_plugin('console.disabled', 'webconsole', {
15                   address = 'wsck:127.0.0.1:3000/murphy',
16                   httpdir = '/usr/share/murphy/webconsole' });
17
18 -- load the dbus plugin
19 if m:plugin_exists('dbus') then
20     m:load_plugin('dbus')
21 end
22
23 -- load the native resource plugin
24 if m:plugin_exists('resource-native') then
25     m:load_plugin('resource-native')
26     m:info("native resource plugin loaded")
27 else
28     m:info("No native resource plugin found...")
29 end
30
31 -- load the dbus resource plugin
32 m:try_load_plugin('resource-dbus', {
33     dbus_bus = "system",
34     dbus_service = "org.Murphy",
35     dbus_track = true,
36     default_zone = "driver",
37     default_class = "implicit"
38 })
39
40 -- load the domain control plugin
41 if m:plugin_exists('domain-control') then
42     m:load_plugin('domain-control')
43 else
44     m:info("No domain-control plugin found...")
45 end
46
47 if m:plugin_exists('glib') then
48     m:load_plugin('glib')
49 else
50     m:info("No glib plugin found...")
51 end
52
53 -- load the AMB plugin
54 if m:plugin_exists('amb') then
55     m:try_load_plugin('amb')
56
57     if builtin.method.amb_initiate and
58        builtin.method.amb_update
59     then
60         with_amb = true
61     end
62 else
63     m:info("No amb plugin found...")
64 end
65
66 -- load the ASM resource plugin
67 if m:plugin_exists('resource-asm') then
68     m:try_load_plugin('resource-asm', {
69         zone = "driver",
70         share_mmplayer = "player:AVP,mandatory,exclusive,strict",
71         ignored_argv0 = "WebProcess"
72     })
73 else
74     m:info("No audio session manager plugin found...")
75 end
76
77 if m:plugin_exists('system-controller') then
78     with_system_controller = true
79 elseif m:plugin_exists('ivi-resource-manager') then
80     m:load_plugin('ivi-resource-manager')
81     with_system_controller = false
82 end
83
84 -- define application classes
85 application_class {
86     name     = "interrupt",
87     priority = 99,
88     modal    = true ,
89     share    = false,
90     order    = "fifo"
91 }
92
93 application_class {
94     name     = "emergency",
95     priority = 80,
96     modal    = false,
97     share    = false,
98     order    = "fifo"
99 }
100 application_class {
101     name     = "system",
102     priority = 52,
103     modal    = false,
104     share    = true,
105     order    = "lifo"
106 }
107 application_class {
108     name     = "alert",
109     priority = 51,
110     modal    = false,
111     share    = false,
112     order    = "fifo"
113 }
114
115 application_class {
116     name     = "navigator",
117     priority = 50,
118     modal    = false,
119     share    = true,
120     order    = "fifo"
121 }
122
123 application_class {
124     name     = "phone",
125     priority = 6 ,
126     modal    = false,
127     share    = true ,
128     order    = "lifo"
129 }
130 application_class {
131     name     = "camera",
132     priority = 5,
133     modal    = false,
134     share    = false,
135     order    = "lifo"
136 }
137
138 application_class { name="event"    , priority=4 , modal=false, share=true , order="fifo" }
139 application_class { name="game"     , priority=3 , modal=false, share=false, order="lifo" }
140 --# doesn't need to be created here, ivi-resource-manager creates it if loaded
141 --#application_class { name="basic"    , priority=2 , modal=false, share=false, order="lifo" }
142 application_class { name="player"   , priority=1 , modal=false, share=true , order="lifo" }
143 application_class { name="implicit" , priority=0 , modal=false, share=false, order="lifo" }
144
145 -- define zone attributes
146 zone.attributes {
147     type = {mdb.string, "common", "rw"},
148     location = {mdb.string, "anywhere", "rw"}
149 }
150
151 -- define zones
152 zone {
153      name = "driver",
154      attributes = {
155          type = "common",
156          location = "front-left"
157      }
158 }
159
160 zone {
161      name = "passanger1",
162      attributes = {
163          type = "private",
164          location = "front-right"
165      }
166 }
167
168 zone {
169      name = "passanger2",
170      attributes = {
171          type = "private",
172          location = "back-left"
173      }
174 }
175
176 zone {
177      name = "passanger3",
178      attributes = {
179          type = "private",
180          location = "back-right"
181      }
182 }
183
184 zone {
185      name = "passanger4",
186      attributes = {
187          type = "private",
188          location = "back-left"
189      }
190 }
191
192
193 -- define resource classes
194 if not m:plugin_exists('ivi-resource-manager') and
195    not with_system_controller
196 then
197    resource.class {
198         name = "audio_playback",
199         shareable = true,
200         attributes = {
201             role = { mdb.string, "music", "rw" },
202             pid = { mdb.string, "<unknown>", "rw" },
203             policy = { mdb.string, "relaxed", "rw" }
204         }
205    }
206 end
207
208 resource.class {
209      name = "audio_recording",
210      shareable = true,
211      attributes = {
212          role   = { mdb.string, "music"    , "rw" },
213          pid    = { mdb.string, "<unknown>", "rw" },
214          policy = { mdb.string, "relaxed"  , "rw" }
215      }
216 }
217
218 resource.class {
219      name = "video_playback",
220      shareable = false,
221 }
222
223 resource.class {
224      name = "video_recording",
225      shareable = false
226 }
227
228 resource.class {
229      name = "speech_recognition",
230      shareable = true
231 }
232
233 resource.class {
234      name = "speech_synthesis",
235      shareable = true
236 }
237
238 if not m:plugin_exists('ivi-resource-manager') and
239    not with_system_controller
240 then
241     resource.method.veto = {
242         function(zone, rset, grant, owners, req_set)
243             return true
244          end
245     }
246 end
247
248 -- test for creating selections
249 mdb.select {
250            name = "audio_owner",
251            table = "audio_playback_owner",
252            columns = {"application_class"},
253            condition = "zone_name = 'driver'"
254 }
255
256 mdb.select {
257            name = "vehicle_speed",
258            table = "amb_vehicle_speed",
259            columns = {"value"},
260            condition = "key = 'VehicleSpeed'"
261 }
262
263 element.lua {
264    name    = "speed2volume",
265    inputs  = { speed = mdb.select.vehicle_speed, param = 9 },
266    outputs = {  mdb.table { name = "speedvol",
267                             index = {"zone", "device"},
268                             columns = {{"zone", mdb.string, 16},
269                                        {"device", mdb.string, 16},
270                                        {"value", mdb.floating}},
271                             create = true
272                            }
273              },
274    oldvolume = 0.0,
275    update  = function(self)
276                 speed = self.inputs.speed.single_value
277                 if (speed) then
278                     volume = (speed - 144.0) / 7.0
279                 else
280                     volume = 0.0
281                 end
282                 diff = volume - self.oldvolume
283                 if (diff*diff > self.inputs.param) then
284                     print("*** element "..self.name.." update "..volume)
285                     self.oldvolume = volume
286                     mdb.table.speedvol:replace({zone = "driver", device = "speakers", value = volume})
287                 end
288              end
289 }
290
291 mdb.select {
292     name = "amb_state",
293     table = "amb_state",
294     columns = { "state" },
295     condition = "id = 0"
296 }
297
298 -- Night mode processing chain
299
300 mdb.select {
301     name = "exterior_brightness",
302     table = "amb_exterior_brightness",
303     columns = { "value" },
304     condition = "key = 'ExteriorBrightness'"
305 }
306
307 element.lua {
308     name    = "nightmode",
309     inputs  = { brightness = mdb.select.exterior_brightness },
310     oldmode = -1;
311     outputs = {
312         mdb.table {
313             name = "amb_nightmode",
314             index = { "id" },
315             create = true,
316             columns = {
317                 { "id", mdb.unsigned },
318                 { "night_mode", mdb.unsigned }
319             }
320         }
321     },
322     update = function(self)
323         -- This is a trivial function to calculate night mode. Later, we will
324         -- need a better threshold value and hysteresis to prevent oscillation.
325
326         brightness = self.inputs.brightness.single_value
327
328         if not brightness then
329             return
330         end
331
332         print("*** element "..self.name.." update brightness: "..brightness)
333
334         if brightness > 300 then
335             mode = 0
336         else
337             mode = 1
338         end
339
340         print("*** resulting mode: ".. mode)
341
342         if not (mode == self.oldmode) then
343             mdb.table.amb_nightmode:replace({ id = 0, night_mode = mode })
344         end
345
346         self.oldmode = mode
347     end
348 }
349
350 mdb.select {
351     name = "select_night_mode",
352     table = "amb_nightmode",
353     columns = { "night_mode" },
354     condition = "id = 0"
355 }
356
357 if with_amb then
358     sink.lua {
359         name = "night_mode",
360         inputs = { NightMode = mdb.select.select_night_mode,
361                    amb_state = mdb.select.amb_state },
362         property = "NightMode",
363         type = "b",
364         initiate = builtin.method.amb_initiate,
365         update = builtin.method.amb_update
366     }
367 end
368
369 -- Night mode general handlers
370
371 if with_system_controller then
372     sink.lua {
373         name = "nightmode_homescreen",
374         inputs = { owner = mdb.select.select_night_mode },
375         initiate = function(self)
376                 -- data = mdb.select.select_night_mode.single_value
377                 return true
378             end,
379         update = function(self)
380                 send_night_mode_to_home_screen()
381             end
382     }
383 end
384
385 -- Driving mode processing chain
386
387 element.lua {
388     name    = "drivingmode",
389     inputs  = { speed = mdb.select.vehicle_speed },
390     oldmode = -1;
391     outputs = {
392         mdb.table {
393             name = "amb_drivingmode",
394             index = { "id" },
395             create = true,
396             columns = {
397                 { "id", mdb.unsigned },
398                 { "driving_mode", mdb.unsigned }
399             }
400         }
401     },
402     update = function(self)
403
404         speed = self.inputs.speed.single_value
405
406         if not speed then
407             return
408         end
409
410         if speed == 0 then
411             mode = 0
412         else
413             mode = 1
414         end
415
416         if not (mode == self.oldmode) then
417             mdb.table.amb_drivingmode:replace({ id = 0, driving_mode = mode })
418         end
419
420         self.oldmode = mode
421     end
422 }
423
424 mdb.select {
425     name = "select_driving_mode",
426     table = "amb_drivingmode",
427     columns = { "driving_mode" },
428     condition = "id = 0"
429 }
430
431 if with_amb then
432     sink.lua {
433         name = "driving_mode",
434         inputs = { DrivingMode = mdb.select.select_driving_mode,
435                    amb_state = mdb.select.amb_state },
436         property = "DrivingMode",
437         type = "u",
438         initiate = builtin.method.amb_initiate,
439         update = builtin.method.amb_update
440     }
441 end
442
443 -- turn signals (left, right)
444
445 mdb.select {
446     name = "winker",
447     table = "amb_turn_signal",
448     columns = { "value" },
449     condition = "key = 'TurnSignal'"
450 }
451
452 -- define three categories
453
454 mdb.select {
455     name = "undefined_applications",
456     table = "aul_applications",
457     columns = { "appid" },
458     condition = "category = '<undefined>'"
459 }
460
461 mdb.select {
462     name = "basic_applications",
463     table = "aul_applications",
464     columns = { "appid" },
465     condition = "category = 'basic'"
466 }
467
468 mdb.select {
469     name = "entertainment_applications",
470     table = "aul_applications",
471     columns = { "appid" },
472     condition = "category = 'entertainment'"
473 }
474
475 function ft(t)
476     -- filter the object garbage out of the tables
477     ret = {}
478
479     for k,v in pairs(t) do
480         if k ~= "userdata" and k ~= "new" then
481             ret[k] = v
482         end
483     end
484
485     return ret
486 end
487
488 function getApplication(appid)
489     local conf = nil
490
491     -- find the correct local application definition
492
493     for k,v in pairs(ft(application)) do
494         if appid == v.appid then
495             conf = v
496             break
497         end
498     end
499
500     return conf
501 end
502
503 function regulateApplications(t, regulation)
504     for k,v in pairs(ft(t)) do
505
506         -- iterate through the undefined and entertainment apps, see if
507         -- they have been overruled in local config
508
509         local conf = getApplication(v.appid)
510
511         if conf and conf.resource_class ~= "player" then
512             -- override, don't disable
513             resmgr:disable_screen_by_appid("*", "*", v.appid, false, false)
514         else
515             resmgr:disable_screen_by_appid("*", "*", v.appid, regulation == 1, false)
516         end
517     end
518     resource.method.recalc("driver")
519 end
520
521 -- regulation (on), use "select_driving_mode"
522
523 sink.lua {
524     name = "driving_regulation",
525     inputs = { owner = mdb.select.select_driving_mode },
526     initiate = function(self)
527         -- local data = mdb.select.select_driving_mode.single_value
528         return true
529     end,
530     update = function(self)
531         local data = mdb.select.select_driving_mode.single_value
532
533         if verbose > 1 then
534             print("Driving mode updated: " .. tostring(data))
535         end
536
537         if not sc then
538             return true
539         end
540
541         -- tell homescreen that driving mode was updated
542         send_driving_mode_to_home_screen()
543
544         regulateApplications(ft(mdb.select.entertainment_applications), data)
545         regulateApplications(ft(mdb.select.undefined_applications), data)
546
547         return true
548     end
549 }
550 --[[
551 sink.lua {
552     name = "regulated_app_change",
553     inputs = { undef = mdb.select.undefined_applications,
554                entertainment = mdb.select.entertainment_applications },
555     initiate = function(self)
556         return true
557     end,
558     update = function(self)
559         local data = mdb.select.select_driving_mode.single_value
560
561         if not sc then
562             return
563         end
564
565         if verbose > 1 then
566             print("regulated application list was changed")
567         end
568
569         regulateApplications(ft(mdb.select.entertainment_applications), data)
570         regulateApplications(ft(mdb.select.undefined_applications), data)
571
572         return true
573     end
574 }
575 --]]
576
577 -- shift position (parking, reverse, other)
578
579 mdb.select {
580     name = "gear_position",
581     table = "amb_gear_position",
582     columns = { "value" },
583     condition = "key = 'GearPosition'"
584 }
585
586 -- cameras (back, front, left, right)
587
588 element.lua {
589     name    = "camera_state",
590     inputs  = { winker = mdb.select.winker, gear = mdb.select.gear_position },
591     oldmode = -1;
592     outputs = {
593         mdb.table {
594             name = "target_camera_state",
595             index = { "id" },
596             create = true,
597             columns = {
598                 { "id", mdb.unsigned },
599                 { "front_camera", mdb.unsigned },
600                 { "back_camera", mdb.unsigned },
601                 { "right_camera", mdb.unsigned },
602                 { "left_camera", mdb.unsigned }
603             }
604         }
605     },
606     update = function(self)
607
608         front_camera = 0
609         back_camera = 0
610         right_camera = 0
611         left_camera = 0
612
613         if self.inputs.gear == 128 then
614             back_camera = 1
615         elseif self.inputs.winker == 1 then
616             right_camera = 1
617         elseif self.inputs.winker == 2 then
618             left_camera = 1
619         end
620
621         mdb.table.target_camera_state:replace({ id = 0, front_camera = front_camera, back_camera = back_camera, right_camera = right_camera, left_camera = left_camera })
622
623     end
624 }
625
626 -- system controller test setup
627
628 if not with_system_controller then
629    -- ok, we should have 'audio_playback' defined by now
630    m:try_load_plugin('telephony')
631    return
632 end
633
634 m:load_plugin('system-controller')
635
636 window_manager_operation_names = {
637     [1] = "create",
638     [2] = "destroy"
639 }
640
641 function window_manager_operation_name(oper)
642     local name = window_manager_operation_names[oper]
643     if name then return name end
644     return "<unknown " .. tostring(oper) .. ">"
645 end
646
647 window_operation_names = {
648     [1] = "create",
649     [2] = "destroy",
650     [3] = "name_change",
651     [4] = "visible",
652     [5] = "configure",
653     [6] = "active",
654     [7] = "map"
655 }
656
657 function window_operation_name(oper)
658     local name = window_operation_names[oper]
659     if name then return name end
660     return "<unknown " .. tostring(oper) .. ">"
661 end
662
663 layer_operation_names = {
664     [1] = "create",
665     [2] = "destroy",
666     [3] = "visible"
667 }
668
669 function layer_operation_name(oper)
670     local name = layer_operation_names[oper]
671     if name then return name end
672     return "<unknown " .. tostring(oper) .. ">"
673 end
674
675 input_manager_operation_names = {
676     [1] = "create",
677     [2] = "destroy",
678     [3] = "ready"
679 }
680
681 function input_manager_operation_name(oper)
682     local name = input_manager_operation_names[oper]
683     if name then return name end
684     return "<unknown " .. tostring(oper) .. ">"
685 end
686
687 input_operation_names = {
688     [1] = "create",
689     [2] = "destroy",
690     [3] = "update"
691 }
692
693 function input_operation_name(oper)
694     local name = input_operation_names[oper]
695     if name then return name end
696     return "<unknown " .. tostring(oper) .. ">"
697 end
698
699 code_operation_names = {
700     [1] = "create",
701     [2] = "destroy",
702     [3] = "state_change"
703 }
704
705 function code_operation_name(oper)
706     local name = code_operation_names[oper]
707     if name then return name end
708     return "<unknown " .. tostring(oper) .. ">"
709 end
710
711 command_names = {
712     [0x00001] = "send_appid",
713     [0x10001] = "create",
714     [0x10002] = "destroy",
715     [0x10003] = "show",
716     [0x10004] = "hide",
717     [0x10005] = "move",
718     [0x10006] = "change_active",
719     [0x10007] = "change_layer",
720     [0x10008] = "change_attr",
721     [0x10009] = "name",
722     [0x10011] = "map_thumb",
723     [0x10012] = "unmap_thumb",
724     [0x10020] = "show layer",
725     [0x10021] = "hide_layer",
726     [0x10022] = "change_layer_attr",
727     [0x20001] = "add_input",
728     [0x20002] = "del_input",
729     [0x20003] = "send_input",
730     [0x40001] = "acquire_res",
731     [0x40002] = "release_res",
732     [0x40003] = "deprive_res",
733     [0x40004] = "waiting_res",
734     [0x40005] = "revert_res",
735     [0x40011] = "create_res",
736     [0x40012] = "destroy_res",
737     [0x50001] = "set_region",
738     [0x50002] = "unset_region",
739     [0x60001] = "change_state"
740 }
741
742 function command_name(command)
743     local name = command_names[command]
744     if name then return name end
745     return "<unknown " .. tostring(command) .. ">"
746 end
747
748 input_layer = {
749    [3] = true, -- input
750    [4] = true, -- touch
751    [5] = true  -- cursor
752 }
753
754 -- some day this should be merged with wmgr.layers
755 ico_layer_type = {
756    [1] = 0x1000, -- background
757    [2] = 0x2000, -- application
758    [3] = 0x4000, -- input
759    [4] = 0xa000, -- touch
760    [5] = 0xb000, -- cursor
761    [6] = 0xc000, -- startup
762    [7] = 0x3000  -- fullscreen
763 }
764
765 resmgr = resource_manager {
766   screen_event_handler = function(self, ev)
767                              local event = ev.event
768                              local surface = ev.surface
769
770                              if event == "init" then
771                                  if verbose > 0 then
772                                      print("*** init screen resource allocation -- disable all 'player'")
773                                  end
774                                  resmgr:disable_audio_by_appid("*", "player", "*", true, false)
775                              elseif event == "preallocate" then
776                                  if verbose > 0 then
777                                      print("*** preallocate screen resource "..
778                                            "for '" .. ev.appid .. "' -- enable 'player', if any")
779                                  end
780                                  resmgr:disable_audio_by_appid("*", "player", ev.appid, false, false)
781                              elseif event == "grant" then
782                                  if verbose > 0 then
783                                     print("*** make visible surface "..surface)
784                                  end
785                                  local a = animation({})
786                                  local r = m:JSON({surface = surface,
787                                                    visible = 1,
788                                                    raise   = 1})
789                                  wmgr:window_request(r,a,0)
790                              elseif event == "revoke" then
791                                  if verbose > 0 then
792                                     print("*** hide surface "..surface)
793                                  end
794                                  local a = animation({})
795                                  local r = m:JSON({surface = ev.surface,
796                                                    visible = 0})
797                                  wmgr:window_request(r,a,0)
798
799                              elseif event == "create" then
800
801                                 if verbose > 0 then
802                                     print("*** screen resource event: " ..
803                                           tostring(ev))
804                                 end
805
806                                 local regulation = mdb.select.select_driving_mode.single_value
807
808                                 if regulation == 1 then
809
810                                     local blacklisted = false
811
812                                     -- applications which have their category set to "entertainment"
813                                     -- or "undefined" are blacklisted, meaning they should be regulated
814
815                                     for i,v in pairs(ft(mdb.select.undefined_applications)) do
816                                         if v.appid == ev.appid then
817                                             if verbose > 0 then
818                                                 print(ev.appid .. " was blacklisted (undefined)")
819                                             end
820                                             blacklisted = true
821                                             break
822                                         end
823                                     end
824
825                                     if not blacklisted then
826                                         for i,v in pairs(ft(mdb.select.entertainment_applications)) do
827                                             if v.appid == ev.appid then
828                                                 if verbose > 0 then
829                                                     print(ev.appid .. " was blacklisted (entertainment)")
830                                                 end
831                                                 blacklisted = true
832                                                 break
833                                             end
834                                         end
835                                     end
836
837                                     -- our local application config, which takes precedence
838                                     local conf = getApplication(ev.appid)
839
840                                     -- disable only non-whitelisted applications
841                                     if not conf or conf.resource_class == "player" then
842                                         if blacklisted then
843                                             if verbose > 0 then
844                                                 print("disabling screen for " .. ev.appid)
845                                             end
846                                             resmgr:disable_screen_by_appid("*", "*", ev.appid, true, true)
847                                         end
848                                     end
849                                 end
850
851                              elseif event == "destroy" then
852                                if verbose > 0 then
853                                     print("*** screen resource event: " ..
854                                           tostring(ev))
855                                  end
856                              else
857                                  if verbose > 0 then
858                                     print("*** screen resource event: " ..
859                                           tostring(ev))
860                                  end
861                              end
862                          end,
863   audio_event_handler = function(self, ev)
864                              local event = ev.event
865                              local appid = ev.appid
866                              local audioid = ev.audioid
867
868                              if event == "grant" then
869                                  if verbose > 0 then
870                                     print("*** grant audio to "..appid..
871                                           " ("..audioid..") in '" ..
872                                           ev.zone .. "' zone")
873                                  end
874                              elseif event == "revoke" then
875                                  if verbose > 0 then
876                                     print("*** revoke audio from "..appid..
877                                           " ("..audioid..") in '" ..
878                                           ev.zone .. "' zone")
879                                  end
880                              else
881                                  if verbose > 0 then
882                                     print("*** audio resource event: " ..
883                                           tostring(ev))
884                                  end
885                              end
886                         end
887 }
888
889 resclnt = resource_client {}
890
891 wmgr = window_manager {
892   geometry = function(self, w,h, v)
893                   if type(v) == "function" then
894                       return v(w,h)
895                   end
896                   return v
897              end,
898
899   application = function(self, appid)
900                      if appid then
901                          local app = application_lookup(appid)
902                          if not app then
903                              app = application_lookup("default")
904                          end
905                          return app
906                      end
907                      return { privileges = {screen="none", audio="none"} }
908                 end,
909
910   output_order = { 1, 0 },
911
912   outputs = { { name  = "Mid",
913                 id    = 0,
914                 zone  = "driver",
915                 areas = { Full = {
916                               id     = 20,
917                               pos_x  = 0,
918                               pos_y  = 0,
919                               width  = function(w,h) return w end,
920                               height = function(w,h) return h end
921                           },
922                           Left = {
923                               id     = 21,
924                               pos_x  = 0,
925                               pos_y  = 0,
926                               width  = 320,
927                               height = function(w,h) return h end
928                           },
929                           Right = {
930                               id     = 22,
931                               pos_x  = function(w,h) return w-320 end,
932                               pos_y  = 0,
933                               width  = 320,
934                               height = function(w,h) return h end
935                           }
936                         }
937                },
938                { name  = "Center",
939                  id    = 1,
940                  zone  = "driver",
941                  areas = { Status = {
942                               id     = 0,
943                               pos_x  = 0,
944                               pos_y  = 0,
945                               width  = function(w,h) return w end,
946                               height = 64
947                            },
948                            Full = {
949                               id     = 1,
950                               pos_x  = 0,
951                               pos_y  = 64,
952                               width  = function(w,h) return w end,
953                               height = function(w,h) return h-64-128 end
954                            },
955                            Upper = {
956                               id     = 2,
957                               pos_x  = 0,
958                               pos_y  = 64,
959                               width  = function(w,h) return w end,
960                               height = function(w,h) return (h-64-128)/2 end
961                            },
962                            Lower = {
963                               id     = 3,
964                               pos_x  = 0,
965                               pos_y  = function(w,h) return (h-64-128)/2+64 end,
966                               width  = function(w,h) return w end,
967                               height = function(w,h) return (h-64-128)/2 end
968                            },
969                            UpperLeft = {
970                               id     = 4,
971                               pos_x  = 0,
972                               pos_y  = 64,
973                               width  = function(w,h) return w/2 end,
974                               height = function(w,h) return (h-64-128)/2 end
975                            },
976                            UpperRight = {
977                               id     = 5,
978                               pos_x  = function(w,h) return w/2 end,
979                               pos_y  = 64,
980                               width  = function(w,h) return w/2 end,
981                               height = function(w,h) return (h-64-128)/2 end
982                            },
983                            LowerLeft = {
984                               id     = 6,
985                               pos_x  = 0,
986                               pos_y  = function(w,h) return (h-64-128/2)+64 end,
987                               width  = function(w,h) return w/2 end,
988                               height = function(w,h) return (h-64-128)/2 end
989                            },
990                            LowerRight = {
991                               id     = 7,
992                               pos_x  = function(w,h) return w/2 end,
993                               pos_y  = function(w,h) return (h-64-128/2)+64 end,
994                               width  = function(w,h) return w/2 end,
995                               height = function(w,h) return (h-64-128)/2 end
996                            },
997                            SysApp = {
998                               id     = 8,
999                               pos_x  = 0,
1000                               pos_y  = 64,
1001                               width  = function(w,h) return w end,
1002                               height = function(w,h) return h-64-128 end
1003                            },
1004                            ["SysApp.Left"] = {
1005                               id     = 9,
1006                               pos_x  = 0,
1007                               pos_y  = 64,
1008                               width  = function(w,h) return w/2-181 end,
1009                               height = function(w,h) return h-64-128 end
1010                            },
1011                            ["SysApp.Right"] = {
1012                               id     = 10,
1013                               pos_x  = function(w,h) return w/2+181 end,
1014                               pos_y  = 64,
1015                               width  = function(w,h) return w/2-181 end,
1016                               height = function(w,h) return h-64-128 end
1017                            },
1018                            Control = {
1019                               id     = 11,
1020                               pos_x  = 0,
1021                               pos_y  = function(w,h) return h-128 end,
1022                               width  = function(w,h) return w end,
1023                               height = 128
1024                            },
1025                         }
1026               }
1027   },
1028   layers = { {      0, "Background"   , 1 },
1029              {      1, "Application"  , 2 },
1030              {      2, "Softkeyboard" , 4 },
1031              {      3, "HomeScreen"   , 2 },
1032              {      4, "ControlBar"   , 2 },
1033              {      5, "InterruptApp" , 2 },
1034              {      6, "OnScreen"     , 2 },
1035              {    101, "Input"        , 3 },
1036              {    102, "Cursor"       , 5 },
1037              {    103, "Startup"      , 6 },
1038              { 0x1000, "Background"   , 1 },
1039              { 0x2000, "Normal"       , 2 },
1040              { 0x3000, "Fullscreen"   , 7 },
1041              { 0x4000, "InputPanel"   , 3 },
1042              { 0xA000, "Touch"        , 4 },
1043              { 0xB000, "Cursor"       , 5 },
1044              { 0xC000, "Startup"      , 6 }
1045   },
1046
1047
1048   manager_update = function(self, oper)
1049                        if verbose > 0 then
1050                            print("### <== WINDOW MANAGER UPDATE:" ..
1051                                  window_manager_operation_name(oper))
1052                        end
1053                        if oper == 1 then
1054                            local wumask = window_mask { raise   = true,
1055                                                         visible = true,
1056                                                         active  = true }
1057                            local wrmask = window_mask { active  = true }
1058                            local lumask = layer_mask  { visible = true }
1059                            local lrmask = layer_mask  { visible = true }
1060                            local req = m:JSON({
1061                                passthrough_window_update  = wumask:tointeger(),
1062                                passthrough_window_request = wrmask:tointeger(),
1063                                passthrough_layer_update   = lumask:tointeger(),
1064                                passthrough_layer_request  = lrmask:tointeger()
1065                            })
1066                            self:manager_request(req)
1067                        end
1068                    end,
1069
1070   window_update = function(self, oper, win, mask)
1071                       if verbose > 0 then
1072                           print("### <== WINDOW UPDATE oper:" ..
1073                                 window_operation_name(oper) ..
1074                                 " mask: " .. tostring(mask))
1075                           if verbose > 1 then
1076                               print(win)
1077                           end
1078                       end
1079
1080                       local arg = m:JSON({ surface = win.surface,
1081                                            winname = win.name,
1082                       })
1083                       local command = 0
1084
1085                       if oper == 1 then -- create
1086                            local layertype = win.layertype
1087                            if layertype and input_layer[layertype] then
1088                                if verbose > 0 then
1089                                    print("ignoring input panel creation")
1090                                end
1091                                return
1092                            end
1093                            command     = 0x10001
1094                       elseif oper == 2 then -- destroy
1095                            command     = 0x10002
1096                       elseif oper == 3 then  -- namechange
1097                            command     = 0x10009
1098                       elseif oper == 4 or oper == 5 then --visible or configure
1099                            local icolayer = ico_layer_type[win.layertype]
1100                            command     = 0x10008
1101                            arg.zone    = win.area
1102                            arg.node    = win.node
1103                            if icolayer then
1104                                arg.layertype = icolayer
1105                            end
1106                            arg.layer   = win.layer
1107                            arg.pos_x   = win.pos_x
1108                            arg.pos_y   = win.pos_y
1109                            arg.width   = win.width
1110                            arg.height  = win.height
1111                            arg.raise   = win.raise
1112                            arg.visible = win.visible
1113                            if win.active == 0 then
1114                                arg.active = 0
1115                            else
1116                                arg.active = 1
1117                            end
1118                       elseif oper == 6 then -- active
1119                            if win.active == 0 then
1120                                if verbose > 0 then
1121                                    print("ignoring inactive event")
1122                                end
1123                                return
1124                            end
1125                            command = 0x10006
1126                       elseif oper == 7 then -- map
1127                            local map = win.map
1128                            if not map then
1129                                return
1130                            end
1131                            if win.mapped == 0 then
1132                                command = 0x10012
1133                            else
1134                                command = 0x10011
1135                            end
1136                            arg.attr = map.type
1137                            arg.name = map.target
1138                            arg.width = map.width
1139                            arg.height = map.height
1140                            arg.stride = map.stride
1141                            arg.format = map.format
1142                       else
1143                            if verbose > 0 then
1144                                print("### nothing to do")
1145                            end
1146                            return
1147                       end
1148
1149                       local msg = m:JSON({ command = command,
1150                                            appid   = win.appid,
1151                                            pid     = win.pid,
1152                                            arg     = arg
1153                       })
1154                       if verbose > 0 then
1155                           print("### <== sending " ..
1156                                 command_name(msg.command) ..
1157                                 " window message to '" .. win.name .. "'")
1158                           if verbose > 1 then
1159                               print(msg)
1160                           end
1161                       end
1162                       sc:send_message(homescreen, msg)
1163
1164                       if oper == 1 then -- create
1165                           local i = input_layer[win.layertype]
1166                           local p = self:application(win.appid)
1167                           local s = p.privileges.screen
1168
1169                           if s == "system" then
1170                               local a = animation({})
1171                               local r = m:JSON({surface = win.surface,
1172                                                 visible = 1,
1173                                                 raise   = 1})
1174                               self:window_request(r,a,0)
1175                           else
1176                               if i then
1177                                   if verbose > 0 then
1178                                       print("do not make resource for " ..
1179                                             "input window")
1180                                   end
1181                               else
1182                                   resclnt:resource_set_create("screen",
1183                                                                "driver",
1184                                                                win.appid,
1185                                                                win.surface)
1186                                   special_screen_sets[win.surface] = true
1187                               end
1188                           end
1189                       elseif oper == 2 then -- destroy
1190                           resclnt:resource_set_destroy("screen", win.surface)
1191                           special_screen_sets[win.surface] = nil
1192                       elseif oper == 6 then -- active
1193                           if win.active then
1194                               local i = input_layer[win.layertype]
1195                               local p = self:application(win.appid)
1196                               local s = p.privileges.screen
1197                               local surface = win.surface
1198                               if not i and s ~= "system" then
1199                                  resclnt:resource_set_acquire("screen",surface)
1200                                  resmgr:window_raise(win.appid, surface, 1)
1201                               end
1202                           end
1203                       end
1204                   end,
1205
1206   layer_update = function(self, oper, layer, mask)
1207                       if verbose > 0 then
1208                           print("### LAYER UPDATE:" ..
1209                                 layer_operation_name(oper) ..
1210                                 " mask: " .. tostring(mask))
1211                           if verbose > 1 then
1212                               print(layer)
1213                           end
1214                       end
1215                       if oper == 3 then -- visible
1216                          local command = 0x10022
1217                          local msg = m:JSON({
1218                                          command = command,
1219                                          appid = "",
1220                                          arg = m:JSON({layer = layer.id,
1221                                                        visible = layer.visible
1222                                          })
1223                                 })
1224                          if verbose > 0 then
1225                             print("### <== sending "..command_name(command)..
1226                                   " layer message")
1227                             if verbose > 1 then
1228                                 print(msg)
1229                             end
1230                          end
1231                          sc:send_message(homescreen, msg)
1232                       else
1233                            if verbose > 0 then
1234                                print("### nothing to do")
1235                            end
1236                       end
1237                  end,
1238
1239   output_update = function(self, oper, out, mask)
1240                       local idx = out.index
1241                       local defidx = self.output_order[idx+1]
1242                       if verbose > 0 then
1243                           print("### OUTPUT UPDATE:" .. oper ..
1244                                 " mask: "..tostring(mask))
1245                       end
1246                       if not defidx then
1247                           return
1248                       end
1249                       print(out)
1250                       local outdef = self.outputs[defidx+1]
1251                       if (oper == 1) then -- create
1252                           if outdef then
1253                               self:output_request(m:JSON({index = idx,
1254                                                           id    = outdef.id,
1255                                                           name  = outdef.name
1256                                                           }))
1257                           end
1258                       elseif (oper == 5) then -- done
1259                           local ads = outdef.areas
1260                           local on = outdef.name
1261                           if ads then
1262                               for name,ad in pairs(ads) do
1263                                   local can = wmgr:canonical_name(on.."."..name)
1264                                   local a = m:JSON({name   = name,
1265                                                     output = out.index})
1266                                   for fld,val in pairs(ad) do
1267                                       a[fld] = self:geometry(out.width,
1268                                                              out.height,
1269                                                              val)
1270                                    end
1271                                    self:area_create(a)
1272                                    resmgr:area_create(area[can], outdef.zone)
1273                               end
1274                           end
1275                       end
1276                   end
1277 }
1278
1279
1280 imgr = input_manager {
1281   inputs = {{ name = "G27 Racing Wheel",
1282               id = 0,
1283               switch = { [2] = {appid="org.tizen.ico.app-soundsample"      },
1284                          [3] = {appid="org.tizen.ico.homescreen", keycode=1},
1285                          [4] = {appid="org.tizen.ico.app-soundsample"      },
1286                          [5] = {appid="org.tizen.ico.homescreen", keycode=2}
1287              }}
1288   },
1289
1290   manager_update = function(self, oper)
1291                        if verbose > 0 then
1292                            print("### <== INPUT MANAGER UPDATE:" ..
1293                                  input_manager_operation_name(oper))
1294                        end
1295                    end,
1296
1297   input_update = function(self, oper, inp, mask)
1298                      if verbose > 0 then
1299                          print("### INPUT UPDATE:" ..
1300                                 input_operation_name(oper) ..
1301                                 " mask: " .. tostring(mask))
1302                           if verbose > 1 then
1303                               print(inp)
1304                           end
1305                       end
1306                  end,
1307   code_update  = function(self, oper, code, mask)
1308                      if verbose > 0 then
1309                          print("### CODE UPDATE: mask: " .. tostring(mask))
1310                          if verbose > 1 then
1311                              print(code)
1312                          end
1313                      end
1314                      local msg = m:JSON({ command = 1,
1315                                           appid = "org.tizen.ico.homescreen",
1316                                           arg = m:JSON({ device = code.device,
1317                                                          time = code.time,
1318                                                          input = code.input,
1319                                                          code = code.id,
1320                                                          state = code.state
1321                                            })
1322                      })
1323                      if verbose > 0 then
1324                          print("### <== sending " ..
1325                                command_name(msg.command) ..
1326                                " input message")
1327                          if verbose > 1 then
1328                              print(msg)
1329                          end
1330                      end
1331                      sc:send_message(homescreen, msg)
1332                  end
1333 }
1334
1335 sc = m:get_system_controller()
1336
1337 -- resource sets
1338 sets = {}
1339
1340 -- special screen resource sets
1341 -- TODO: just rewrite screen resource handling to use regular resource API
1342
1343 special_screen_sets = {}
1344
1345 -- user manager
1346 um = m:UserManager()
1347
1348 connected = false
1349 homescreen = ""
1350 onscreen = ""
1351
1352 -- these shoud be before wmgr:connect() is called
1353 if verbose > 0 then
1354    print("====== creating applications ======")
1355 end
1356 application {
1357     appid          = "default",
1358     area           = "Center.Full",
1359     privileges     = { screen = "none", audio = "none" },
1360     resource_class = "player",
1361     screen_priority = 0
1362 }
1363
1364 application {
1365     appid           = "weston",
1366     area            = "Center.Full",
1367     privileges      = { screen = "system", audio = "none" },
1368     resource_class  = "implicit",
1369     screen_priority = 30
1370 }
1371
1372 application {
1373     appid           = "org.tizen.ico.homescreen",
1374     area            = "Center.Full",
1375     windows         = { {'ico_hs_controlbarwindow', 'Center.Control'} },
1376     privileges      = { screen = "system", audio = "system" },
1377     resource_class  = "player",
1378     screen_priority = 20
1379 }
1380
1381 application {
1382     appid           = "org.tizen.ico.statusbar",
1383     area            = "Center.Status",
1384     privileges      = { screen = "system", audio = "none" },
1385     resource_class  = "player",
1386     screen_priority = 20
1387 }
1388
1389 application {
1390     appid           = "org.tizen.ico.onscreen",
1391     area            = "Center.Full",
1392     privileges      = { screen = "system", audio = "system" },
1393     resource_class  = "player",
1394     screen_priority = 20
1395 }
1396
1397 application {
1398     appid           = "org.tizen.ico.login",
1399     area            = "Center.Full",
1400     privileges      = { screen = "system", audio = "system" },
1401     resource_class  = "player",
1402     screen_priority = 20
1403 }
1404
1405 application {
1406     appid           = "org.tizen.ico.camera_left",
1407     area            = "Center.SysApp.Left",
1408     privileges      = { screen = "system", audio = "none" },
1409     requisites      = { screen = "blinker_left", audio = "none" },
1410     resource_class  = "player",
1411     screen_priority = 30
1412 }
1413
1414 application {
1415     appid           = "org.tizen.ico.camera_right",
1416     area            = "Center.SysApp.Right",
1417     privileges      = { screen = "system", audio = "none" },
1418     requisites      = { screen = "blinker_right", audio = "none" },
1419     resource_class  = "player",
1420     screen_priority = 30
1421 }
1422
1423 application {
1424     appid           = "net.zmap.navi",
1425     area            = "Center.Full",
1426     privileges      = { screen = "none", audio = "none" },
1427     resource_class  = "navigator",
1428     screen_priority = 30
1429 }
1430
1431 application {
1432     appid           = "GV3ySIINq7.GhostCluster",
1433     area            = "Mid.Full",
1434     privileges      = { screen = "none", audio = "none" },
1435     resource_class  = "system",
1436     screen_priority = 30
1437 }
1438
1439 if sc then
1440     sc.client_handler = function (self, cid, msg)
1441         local command = msg.command
1442         local appid = msg.appid
1443         if verbose > 0 then
1444             print('### ==> client handler:')
1445             if verbose > 1 then
1446                 print(msg)
1447             end
1448         end
1449
1450         -- known commands: 1 for SEND_APPID, synthetic command 0xFFFF for
1451         -- disconnection
1452
1453         if command == 0xFFFF then
1454             if verbose > 1 then
1455                 print('client ' .. cid .. ' (' .. msg.appid .. ') disconnected')
1456             end
1457             if msg.appid == homescreen then
1458                 homescreen = ""
1459                 for i,v in pairs(special_screen_sets) do
1460                     resclnt:resource_set_destroy("screen", i)
1461                     special_screen_sets[i] = nil
1462                 end
1463             end
1464             return
1465         end
1466
1467         -- handle the connection to weston
1468
1469         if appid then
1470             if appid == "org.tizen.ico.homescreen" then
1471                 print('Setting homescreen='..appid)
1472                 homescreen = appid
1473                 if command and command == 1 then
1474                     send_driving_mode_to_home_screen()
1475                     send_night_mode_to_home_screen()
1476                 end
1477             elseif appid == "org.tizen.ico.onscreen" then
1478                 onscreen = appid
1479             end
1480
1481             if not connected and appid == "org.tizen.ico.homescreen" then
1482                 print('Trying to connect to weston...')
1483                 connected = wmgr:connect()
1484             end
1485         end
1486     end
1487
1488     sc.generic_handler = function (self, cid, msg)
1489         if verbose > 0 then
1490             print('### ==> generic handler:')
1491             if verbose > 1 then
1492                print(msg)
1493             end
1494         end
1495     end
1496
1497     sc.window_handler = function (self, cid, msg)
1498         if verbose > 0 then
1499             print('### ==> received ' ..
1500                    command_name(msg.command) .. ' message')
1501             if verbose > 1 then
1502                 print(tostring(msg))
1503             end
1504         end
1505
1506         local a = animation({})
1507         local nores = false
1508         if msg.command == 0x10003 then       -- ico SHOW command
1509             local raise_mask = 0x01000000
1510             local lower_mask = 0x02000000
1511             local nores_mask = 0x40000000
1512             local time_mask  = 0x00ffffff
1513             msg.arg.visible = 1
1514             if msg.arg then
1515                 local time = 200
1516                 if  msg.arg.anim_time then
1517                     local t = msg.arg.anim_time
1518                     time = m:AND(t, time_mask)
1519                     nores = not m:AND(t, nores_mask)
1520                     if m:AND(t, raise_mask) then
1521                         msg.arg.raise = 1
1522                     elseif m:AND(t, lower_mask) then
1523                         msg.arg.raise = 0
1524                     end
1525                 end
1526                 if msg.arg.anim_name then
1527                     a.show = { msg.arg.anim_name, time }
1528                     print('time: ' .. tostring(a.show[2]))
1529                 end
1530             end
1531             if not nores then
1532                 local p = wmgr:application(msg.appid)
1533                 local s = p.privileges.screen
1534                 if s == "system" then
1535                     nores = true
1536                     if not msg.arg.raise then
1537                         msg.arg.raise = 1
1538                     end
1539                 end
1540             end
1541             if verbose > 2 then
1542                 print('### ==> SHOW')
1543                 print(tostring(msg.arg))
1544             end
1545             if nores then
1546                 wmgr:window_request(msg.arg, a, 0)
1547             else
1548                 local surface = msg.arg.surface
1549                 resclnt:resource_set_acquire("screen", surface)
1550                 resmgr:window_raise(msg.appid, surface, 1)
1551             end
1552         elseif msg.command == 0x10004 then   -- ico HIDE command
1553             local raise_mask = 0x01000000
1554             local lower_mask = 0x02000000
1555             local nores_mask = 0x40000000
1556             local time_mask  = 0x00ffffff
1557             msg.arg.visible = 0
1558             if msg.arg then
1559                 local time = 200
1560                 if msg.arg.anim_time then
1561                     local t = msg.arg.anim_time
1562                     time = m:AND(t, time_mask)
1563                     nores = not m:AND(t, nores_mask)
1564                 end
1565                 if msg.arg.anim_name then
1566                     a.hide = { msg.arg.anim_name, time }
1567                     print('hide animation time: ' .. tostring(a.hide[2]))
1568                 end
1569             end
1570             if not nores then
1571                 local p = wmgr:application(msg.appid)
1572                 local s = p.privileges.screen
1573                 if s == "system" then
1574                     nores = true
1575                     msg.arg.raise = 0
1576                 end
1577             end
1578             if verbose > 2 then
1579                 print('### ==> HIDE REQUEST')
1580                 print(tostring(msg.arg))
1581             end
1582             if nores then
1583                 wmgr:window_request(msg.arg, a, 0)
1584             else
1585                 resmgr:window_raise(msg.appid, msg.arg.surface, -1)
1586             end
1587         elseif msg.command == 0x10005 then   -- ico MOVE
1588             if verbose > 2 then
1589                 print('### ==> MOVE REQUEST')
1590                 print(tostring(msg.arg))
1591             end
1592             if msg.arg.zone then
1593                 msg.arg.area = msg.arg.zone
1594             end
1595             wmgr:window_request(msg.arg, a, 0)
1596             -- TODO: handle if area changed
1597         elseif msg.command == 0x10006 then   -- ico ACTIVE
1598             if not msg.arg.active then
1599                 msg.arg.active = 3 -- pointer + keyboard
1600             end
1601             if verbose > 2 then
1602                 print('### ==> ACTIVE REQUEST')
1603                 print(tostring(msg.arg))
1604             end
1605             wmgr:window_request(msg.arg, a, 0)
1606         elseif msg.command == 0x10007 then   -- ico CHANGE_LAYER
1607             if verbose > 2 then
1608                 print('### ==> CHANGE_LAYER REQUEST')
1609                 print(tostring(msg.arg))
1610             end
1611             --[[
1612             if msg.arg.layer ~= 4 or msg.arg.layer ~= 5 then
1613                 print("do not change layer for other than cursor or touch")
1614                 return
1615             end
1616             --]]
1617             wmgr:window_request(msg.arg, a, 0)
1618         elseif msg.command == 0x10011 then   -- ico MAP_THUMB
1619             local framerate = msg.arg.framerate
1620             if not framerate or framerate < 0 then
1621                 framerate = 0
1622             end
1623             msg.arg.mapped = 1
1624             if verbose > 2 then
1625                 print('### ==> MAP_THUMB REQUEST')
1626                 print(msg.arg)
1627                 print('framerate: '..framerate)
1628             end
1629             --wmgr:window_request(msg.arg, a, framerate)
1630         elseif msg.command == 0x10012 then   -- ico UNMAP_THUMB
1631             msg.arg.mapped = 0
1632             if verbose > 2 then
1633                 print('### ==> UNMAP_THUMB REQUEST')
1634                 print(msg.arg)
1635             end
1636             --wmgr:window_request(msg.arg, a, 0)
1637         elseif msg.command == 0x10020 then   -- ico SHOW_LAYER command
1638             msg.arg.visible = 1
1639             if verbose > 2 then
1640                 print('### ==> SHOW_LAYER REQUEST')
1641                 print(msg.arg)
1642             end
1643             wmgr:layer_request(msg.arg)
1644         elseif msg.command == 0x10021 then   -- ico HIDE_LAYER command
1645             msg.arg.visible = 0
1646             if verbose > 2 then
1647                 print('### ==> HIDE_LAYER REQUEST')
1648                 print(msg.arg)
1649             end
1650             wmgr:layer_request(msg.arg)
1651         end
1652     end
1653
1654     sc.input_handler = function (self, cid, msg)
1655         if verbose > 0 then
1656             print('### ==> input handler: ' .. command_name(msg.command))
1657             if verbose > 1 then
1658                 print(msg)
1659             end
1660         end
1661         if msg.command == 0x20001 then -- add_input
1662             msg.arg.appid = msg.appid
1663             if verbose > 2 then
1664                 print('### ==> ADD_INPUT REQUEST')
1665                 print(tostring(msg.arg))
1666             end
1667             imgr:input_request(msg.arg)
1668         elseif msg.command == 0x20002 then -- del_input
1669             msg.arg.appid = ''
1670             if verbose > 2 then
1671                 print('### ==> DEL_INPUT REQUEST')
1672                 print(tostring(msg.arg))
1673             end
1674             imgr:input_request(msg.arg)
1675         elseif msg.command == 0x20003 then -- send_input
1676         end
1677     end
1678
1679     sc.user_handler = function (self, cid, msg)
1680         if verbose > 0 then
1681             print('### ==> user handler: ' .. command_name(msg.command))
1682             if verbose > 1 then
1683                 print(msg)
1684             end
1685         end
1686
1687         if not um then
1688             print("User Manager not initialized")
1689             return
1690         end
1691
1692         if msg.command == 0x00030001 then -- MSG_CMD_CHANGE_USER
1693             print("command CHANGE_USER")
1694             if not msg.arg then
1695                 print("invalid message")
1696                 return
1697             end
1698
1699             username = msg.arg.user
1700             passwd = msg.arg.pass
1701
1702             if not username then
1703                 username = ""
1704             end
1705
1706             if not passwd then
1707                 passwd = ""
1708             end
1709
1710             success = um:changeUser(username, passwd)
1711
1712             if not success then
1713                 reply = m.JSON({
1714                     appid = msg.appid,
1715                     command = cmd
1716                 })
1717                 if sc:send_message(msg.appid, reply) then
1718                     print('*** sent authentication failed message')
1719                 else
1720                     print('*** failed to send authentication failed message')
1721                 end
1722             end
1723
1724         elseif msg.command == 0x00030002 then -- MSG_CMD_GET_USERLIST
1725             print("command GET_USERLIST")
1726             if not msg.appid then
1727                 print("invalid message")
1728                 return
1729             end
1730
1731             users, currentUser = um:getUserList()
1732
1733             if not users then
1734                 print("failed to get user list")
1735                 return
1736             end
1737
1738             nUsers = 0
1739
1740             for i,v in pairs(users) do
1741                 nUsers = nUsers + 1
1742             end
1743
1744             if not currentUser then
1745                 currentUser = ""
1746             end
1747
1748             if verbose > 1 then
1749                 print("current user: " .. currentUser)
1750                 print("user list:")
1751                 for i,v in pairs(users) do
1752                     print(v)
1753                 end
1754             end
1755
1756             reply = m.JSON({
1757                 appid = msg.appid,
1758                 command = cmd,
1759                 arg = m.JSON({
1760                     user_num = nUsers,
1761                     user_list = users,
1762                     user_login = currentUser
1763                 })
1764             })
1765
1766             if verbose > 1 then
1767                 print("### <== GetUserList reply: " .. tostring(reply))
1768             end
1769
1770             if sc:send_message(msg.appid, reply) then
1771                 print('*** reply OK')
1772             else
1773                 print('*** reply FAILED')
1774             end
1775
1776         elseif msg.command == 0x00030003 then -- MSG_CMD_GET_LASTINFO
1777             print("command GET_LASTINFO")
1778             if not msg.appid then
1779                 print("invalid message")
1780                 return
1781             end
1782
1783             lastInfo = um:getLastInfo(msg.appid)
1784
1785             if not lastInfo then
1786                 print("failed to get last info for app" .. msg.appid)
1787                 return
1788             end
1789
1790             reply = m.JSON({
1791                 appid = msg.appid,
1792                 command = cmd,
1793                 arg = m.JSON({
1794                     lastinfo = lastinfo
1795                 })
1796             })
1797
1798             if sc:send_message(msg.appid, reply) then
1799                 print('*** reply OK')
1800             else
1801                 print('*** reply FAILED')
1802             end
1803
1804         elseif msg.command == 0x00030004 then -- MSG_CMD_SET_LASTINFO
1805             print("command SET_LASTINFO")
1806             if not msg.arg or not msg.appid then
1807                 print("invalid message")
1808                 return
1809             end
1810
1811             lastInfo = um:setLastInfo(msg.appid, msg.arg.lastinfo)
1812         end
1813     end
1814
1815     sc.resource_handler = function (self, cid, msg)
1816         if verbose > 0 then
1817             print('### ==> resource handler: ' .. command_name(msg.command))
1818             if verbose > 1 then
1819                 print(msg)
1820             end
1821         end
1822
1823         createResourceSet = function (ctl, client, msg)
1824             cb = function(rset, data)
1825                 print("> resource callback")
1826
1827                 -- type is either basic (0) or interrupt (1)
1828                 requestType = 0
1829                 if msg.res.type then
1830                     requestType = msg.res.type
1831                 end
1832
1833                 if rset.acquired then
1834                     cmd = 0x00040001 -- acquire
1835                 else
1836                     cmd = 0x00040002 -- release
1837                 end
1838
1839                 reply = m.JSON({
1840                         appid = data.client,
1841                         command = cmd,
1842                         res = {
1843                             type = requestType
1844                         }
1845                     })
1846
1847                 if rset.resources.audio_playback then
1848                     reply.res.sound = {
1849                         zone = "driver",
1850                         name = msg.appid,
1851                         adjust = 0,
1852                         -- id = "0"
1853                     }
1854                 end
1855
1856                 if rset.resources.display then
1857                     reply.res.window = {
1858                         zone = "driver",
1859                         name = msg.appid,
1860                         -- id = "0"
1861                     }
1862                 end
1863
1864                 if rset.resources.input then
1865                     reply.res.input = {
1866                         name = msg.appid,
1867                         event = 0
1868                     }
1869                 end
1870                 print("sending message to client: " .. data.client)
1871
1872                 if sc:send_message(data.client, reply) then
1873                     print('*** reply OK')
1874                 else
1875                     print('*** reply FAILED')
1876                 end
1877             end
1878
1879             rset = m:ResourceSet({
1880                     application_class = "player",
1881                     zone = "driver", -- msg.zone ("full")
1882                     callback = cb
1883                 })
1884
1885             rset.data = {
1886                 cid = cid,
1887                 ctl = ctl
1888             }
1889
1890             if msg.res.sound then
1891                 rset:addResource({
1892                         resource_name = "audio_playback"
1893                     })
1894                 rset.resources.audio_playback.attributes.pid = tostring(msg.pid)
1895                 rset.resources.audio_playback.attributes.appid = msg.appid
1896                 print("sound name: " .. msg.res.sound.name)
1897                 print("sound zone:" .. msg.res.sound.zone)
1898                 print("sound adjust: " .. tostring(msg.res.sound.adjust))
1899                 if msg.res.sound.id then
1900                     print("sound id: " .. msg.res.sound.id)
1901                 end
1902             end
1903
1904             if msg.res.input then
1905                 rset:addResource({
1906                         resource_name = "input"
1907                     })
1908                 rset.resources.input.attributes.pid = tostring(msg.pid)
1909                 rset.resources.input.attributes.appid = msg.appid
1910                 print("input name: " .. msg.res.sound.name)
1911                 print("input event:" .. tostring(msg.res.input.event))
1912             end
1913
1914             if msg.res.window then
1915                 rset:addResource({
1916                         resource_name = "display"
1917                     })
1918                 rset.resources.display.attributes.pid = tostring(msg.pid)
1919                 rset.resources.display.attributes.appid = msg.appid
1920                 print("display name: " .. msg.res.display.name)
1921                 print("display zone:" .. msg.res.display.zone)
1922                 if msg.res.display.id then
1923                     print("display id: " .. msg.res.display.id)
1924                 end
1925             end
1926
1927             return rset
1928         end
1929
1930         -- parse the message
1931
1932         -- fields common to all messages:
1933         --      msg.command
1934         --      msg.appid
1935         --      msg.pid
1936
1937         if msg.command == 0x00040011 then -- MSG_CMD_CREATE_RES
1938             print("command CREATE")
1939
1940             if not sets.cid then
1941                 sets.cid = createResourceSet(self, cid, msg)
1942             end
1943
1944         elseif msg.command == 0x00040012 then -- MSG_CMD_DESTORY_RES
1945             print("command DESTROY")
1946
1947             if sets.cid then
1948                 sets.cid:release()
1949             end
1950
1951             sets.cid = nil -- garbage collecting
1952
1953         elseif msg.command == 0x00040001 then -- MSG_CMD_ACQUIRE_RES
1954             print("command ACQUIRE")
1955
1956             if not sets.cid then
1957                 sets.cid = createResourceSet(self, cid, msg)
1958             end
1959
1960             sets.cid:acquire()
1961
1962         elseif msg.command == 0x00040002 then -- MSG_CMD_RELEASE_RES
1963             print("command RELEASE")
1964
1965             if sets.cid then
1966                 sets.cid:release()
1967             end
1968
1969         elseif msg.command == 0x00040003 then -- MSG_CMD_DEPRIVE_RES
1970             print("command DEPRIVE")
1971
1972         elseif msg.command == 0x00040004 then -- MSG_CMD_WAITING_RES
1973             print("command WAITING")
1974
1975         elseif msg.command == 0x00040005 then -- MSG_CMD_REVERT_RES
1976             print("command REVERT")
1977         end
1978     end
1979
1980     sc.inputdev_handler = function (self, cid, msg)
1981         if verbose > 0 then
1982             print('*** inputdev handler: ' .. command_name(msg.command))
1983             if verbose > 1 then
1984                 print(msg)
1985             end
1986         end
1987     end
1988 end
1989
1990 function send_driving_mode_to_home_screen()
1991     if homescreen == "" then
1992         return
1993     end
1994
1995     local driving_mode = mdb.select.select_driving_mode.single_value
1996
1997     if not driving_mode then driving_mode = 0 end
1998
1999     local reply = m:JSON({ command = 0x60001,
2000                            arg     = m:JSON({ stateid = 1,
2001                                               state   = driving_mode
2002                                      })
2003                   })
2004
2005     if verbose > 0 then
2006         print("### <== sending " .. command_name(reply.command) .. " message")
2007         if verbose > 1 then
2008             print(reply)
2009         end
2010     end
2011
2012     sc:send_message(homescreen, reply)
2013 end
2014
2015 function send_night_mode_to_home_screen()
2016     if homescreen == "" then
2017         return
2018     end
2019
2020     local night_mode = mdb.select.select_night_mode.single_value
2021
2022     if not night_mode then night_mode = 0 end
2023
2024     local reply = m:JSON({ command = 0x60001,
2025                            arg     = m:JSON({ stateid = 2,
2026                                               state   = night_mode
2027                                      })
2028                   })
2029
2030     if verbose > 0 then
2031         print("### <== sending " .. command_name(reply.command) .. " message")
2032         if verbose > 1 then
2033             print(reply)
2034         end
2035      end
2036
2037      sc:send_message(homescreen, reply)
2038 end
2039
2040 -- we should have 'audio_playback' defined by now
2041 m:try_load_plugin('telephony')